package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.ArticleBean;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ReportBean;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ShareBean;
import com.uchengwang.ui.cm_ui.cycle.bean.SetupMenusBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.MoreOperateUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/6/14.
 */

public class TalentInfosAdapter extends RecyclerView.Adapter<TalentInfosAdapter.TalentInfosVH> {
    private Context context;
    private View bottomView;
    private List<ArticleBean> data = new ArrayList<>();

    public List<ArticleBean> getData() {
        return data;
    }

    public void setData(List<ArticleBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setBottomView(View bottomView){
        this.bottomView = bottomView;
    }

    public TalentInfosAdapter (Context context){
        this.context = context;
    }

    @Override
    public TalentInfosAdapter.TalentInfosVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_talent_infos, parent, false);
        return new TalentInfosVH(view);
    }

    @Override
    public void onBindViewHolder(final TalentInfosAdapter.TalentInfosVH holder, final int position) {
        final ArticleBean bean = data.get(position);

        ImageManager.getInstance().loadImage(context, holder.head, bean.getUserAvatar(), ImageManager.ImageType.CIRCLE);
        ImageManager.getInstance().loadImage(context, holder.cover, bean.getArticleCover(), ImageManager.ImageType.ROUND);
        holder.title.setText(bean.getArticleTitle());
        holder.school.setText(bean.getUniName());
        holder.name.setText(bean.getNickName());

        holder.desc.setText(bean.getArticleContent());

        if (bean.getTotalScore() >0){
            holder.rating_num.setText(bean.getTotalScore() + "星光");
        }else {
            holder.rating_num.setText( "" );
        }

        if (bean.getTalentScore() == null){
            //说明没有给改文章打分
            holder.ratingBar.setRating(0);
            holder.ratingBar.setIsIndicator(false);
        }else {
            //说明已打分 不能再打
            holder.ratingBar.setIsIndicator(true);
            holder.ratingBar.setRating(bean.getTalentScore());
        }

        holder.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, final float rating, boolean fromUser) {
                if (rating <=0){
                    return;
                }
                if (bean.getTalentScore() != null){
                    //说是初始设置分值 不是打分
                    return;
                }

                CycleHttpUtils.getUtils().articleScore((Activity) context, bean.getId(), bean.getUid(), (int) rating, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            UCommonUtil.getUtil().showToast(context, "打分成功");
                            holder.ratingBar.setIsIndicator(true);
                            holder.ratingBar.setRating(rating);
                            bean.setTalentScore((int) rating);
                        }else {
                            UCommonUtil.getUtil().showToast(context, "请重试");
                        }
                    }
                });
            }
        });

        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetupMenusBean setupMenusBean = new SetupMenusBean();
                H5ShareBean h5ShareBean = new H5ShareBean();
                h5ShareBean.setCover(bean.getArticleCover());
                h5ShareBean.setTitle(bean.getArticleTitle());
                h5ShareBean.setDesc(bean.getArticleContent());
                h5ShareBean.setUrl(BaseConfig.BaseWebURL + "/article/" + bean.getId());
                setupMenusBean.setShare(h5ShareBean);

                H5ReportBean h5ReportBean = new H5ReportBean();
                h5ReportBean.setReferId(bean.getId());
                h5ReportBean.setReferType(BaseConfig.SHARE_ARTICLE);
                h5ReportBean.setReportUid(bean.getUid());
                h5ReportBean.setReportNickName(bean.getNickName());
                h5ReportBean.setReportUserAvatar(bean.getUserAvatar());

                setupMenusBean.setReport(h5ReportBean);

                MoreOperateUtils.getUtils().more((Activity) context, bottomView, setupMenusBean);
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class TalentInfosVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView cover, head, more;
        private TextView title, desc, rating_num, name, school;
        private RatingBar ratingBar;

        public TalentInfosVH(View view) {
            super(view);
            AutoUtils.auto(view);

            itemView = view;
            cover = view.findViewById(R.id.cover);
            title = view.findViewById(R.id.title);

            head = view.findViewById(R.id.head);
            more = view.findViewById(R.id.more);
            desc = view.findViewById(R.id.desc);
            rating_num = view.findViewById(R.id.rating_num);
            name = view.findViewById(R.id.name);
            school = view.findViewById(R.id.school);
            ratingBar = view.findViewById(R.id.ratingBar);
        }
    }
}
